package xyz.luhui1hao.collection.map;

import java.util.*;

/**
 * @ClassName MapMain
 * @Description TreeMap与HashMap的区别和共同点
 * https://blog.csdn.net/zxw9202/article/details/79097410
 *
 * @Author luhui1hao
 * @Date 2018/9/26 4:55 PM
 * @Version 1.0
 */
public class MapMain {
    public static void main(String[] args) {
        hashMapFun();
        System.out.println("################");
        treeMapFun();
        System.out.println("################");
        linkedHashMap();
    }

    public static void hashMapFun(){
        HashMap hashMap = new HashMap();
        hashMap.put("edgd", 5);
        hashMap.put("chhdd", 3);
        hashMap.put("bdd", 2);
        hashMap.put("afff", 1);
        hashMap.put("drtgt", 4);

        for (Iterator iterator = hashMap.entrySet().iterator(); iterator.hasNext(); ) {
            Map.Entry entry = (Map.Entry) iterator.next();
            System.out.println(entry.getKey() + "=" + entry.getValue());
        }

        System.out.println("---------------");

        for(Iterator iterator = hashMap.keySet().iterator(); iterator.hasNext();){
            String key = (String) iterator.next();
            System.out.println(hashMap.get(key));
        }
    }

    public static void treeMapFun(){
        TreeMap treeMap = new TreeMap();
        treeMap.put("edgd", 5);
        treeMap.put("chhdd", 3);
        treeMap.put("bdd", 2);
        treeMap.put("afff", 1);
        treeMap.put("drtgt", 4);

        for (Iterator iterator = treeMap.entrySet().iterator(); iterator.hasNext(); ) {
            Map.Entry entry = (Map.Entry) iterator.next();
            System.out.println(entry.getKey() + "=" + entry.getValue());
        }

        System.out.println("---------------");

        for(Iterator iterator = treeMap.keySet().iterator(); iterator.hasNext();){
            String key = (String) iterator.next();
            System.out.println(treeMap.get(key));
        }
    }

    public static void linkedHashMap(){
        LinkedHashMap linkedHashMap = new LinkedHashMap();
        linkedHashMap.put("edgd", 5);
        linkedHashMap.put("chhdd", 3);
        linkedHashMap.put("bdd", 2);
        linkedHashMap.put("afff", 1);
        linkedHashMap.put("drtgt", 4);

        for (Iterator iterator = linkedHashMap.entrySet().iterator(); iterator.hasNext(); ) {
            Map.Entry entry = (Map.Entry) iterator.next();
            System.out.println(entry.getKey() + "=" + entry.getValue());
        }

        System.out.println("---------------");

        for(Iterator iterator = linkedHashMap.keySet().iterator(); iterator.hasNext();){
            String key = (String) iterator.next();
            System.out.println(linkedHashMap.get(key));
        }
    }
}
